const Core = require('@alicloud/pop-core');
const rn = require("randomnumber");
const jwt = require("jsonwebtoken");

module.exports={
  sendMsg(tel,code){
    var client = new Core({
      accessKeyId: 'LTAI4G6Gap7intXLgg7yxi8X',
      accessKeySecret: 'CyMmspOeZjyQCBImYGLjEFpiRNxbY5',
      endpoint: 'https://dysmsapi.aliyuncs.com',
      apiVersion: '2017-05-25'
    });
    
    var params = {
      "RegionId": "cn-hangzhou",
      "PhoneNumbers": tel,
      "SignName": "第二天堂",
      "TemplateCode": "SMS_204287827",
      "TemplateParam": `{code:${code}}`
    }
    
    var requestOption = {
      method: 'POST'
    };
    return new Promise((resolve,reject)=>{
      client.request('SendSms', params, requestOption).then((result) => {
        console.log(JSON.stringify(result));
        resolve();
      }, (ex) => {
        console.log(ex);
        reject()
      })
    })
  },
  randomNum(n){
    return rn.generate(n);
  },
  verify(t){
      return new Promise((resolve,reject)=>{
          jwt.verify(t,"千锋H5，牛！", (err,data)=>{
              if(err) reject();
              resolve(data);
          })
      })
  }
}
